package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "04. 类别管理模块")
@RestController
@RequestMapping("/categorys")
public class CategoryController {

    @Autowired
    ICategoryService categoryService;

    public CategoryController(){
        System.out.println("创建控制类对象: CategoryController");
    }

    // http://localhost:8080/categorys/add-new?id=1
    @PostMapping("/add-new")
    @ApiOperation("添加类别")//将添加类别替换成addNew
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated CategoryAddNewDTO categoryAddNewDTO){
        /*try {
            categoryService.addNew(categoryAddNewDTO);
            return "添加类别名称成功";
        }catch (ServiceException e){
            return "添加类别名称失败,类别名称已被占用";
        }*/
        log.debug("开始处理[属性模板]的请求,参数: {}", categoryAddNewDTO);
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/categorys/delete?id=1
    @ApiOperation("删除类别")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete")
    public JsonResult delete(Long id){
        log.debug("开始处理[删除类别数据]的请求,参数: {}",id);
        categoryService.delete(id);
        return JsonResult.ok();
    }
}
